// nav.hover
let ospan = document.querySelectorAll(".ml_12 span");
let ass = document.getElementsByClassName("ass");
let toggle = document.getElementById('toggle')
ass.onmouseover = function() {
    let bao = document.getElementsByClassName("bao");
    bao.style.display = "block";
};
for (let i = 0; i < ospan.length; i++) {
    let price = ospan[i].getAttribute("class") || "";
    let str = price + " navHover";
    ospan[i].setAttribute("class", str);
}
//轮播图
let slider1 = new Slider("sliderBox", 520, 280);
slider1.addArr();
slider1.addFocus();

var swiper = new Swiper(".mySwiper", {
    autoplay: true, //可选选项，自动滑动
    autoplay: {
        delay: 3000,
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
});

//渲染列表
let oUl1 = document.getElementById("oUl1");
let oul = document.getElementById("oUl");
let loveList = document.getElementById("loveList");
// let url = 'http://localhost:1337'
let url = "http://192.168.1.35:1337";

function renderList(dom, num, count) {
    $.get(url + "/api/v1/products", {
        category: num,
        per: count
    }).then((r) => {
        console.log(r.data)
        let src = "";
        r.data.forEach((v) => {
            src += `
                             <li class="oLi" onclick='details(${v.id})'>
                         <img src="${url+v.coverImage}" alt="">
                         <p>${v.name}</p>
                         <span>${"￥" + v.price}</span>
                         </li>
                             `;
        });
        dom.innerHTML = src;


    });
}

renderList(oUl1, 6, 8);
renderList(oul, 5, 10);
renderList(loveList, 7, 20);

// 跳转详情页
function details(id) {
    location.href = "Details.html?id=" + id;
}

// 跳转页面
let a = localStorage.getItem("username");
let names = document.getElementById("names");
let pd = document.getElementById("pd");
let scr = "";
if (a) {
    pd.style.display = "none";
    scr += `
   <span>${"你好" + a}</span>
                   
   `;
    names.innerHTML = scr;
    toggle.innerText = '切换用户'
    let ml = document.querySelectorAll(".ml_12");
    for (let i = 0; i < ml.length; i++) {
        ml[i].onclick = function() {
            location.href = "cart.html";
        };
    }
} else {
    let ml = document.querySelectorAll(".ml_12");
    for (let i = 0; i < ml.length; i++) {
        ml[i].onclick = function() {
            location.href = "login.html";
        };
    }
}
// 回到顶部
let hddb = document.getElementById("hddb");
let oli = hddb.children;
console.log(oli);
for (let i = 0; i < oli.length; i++) {
    oli[i].onclick = function() {
        var a = document.documentElement.scrollTop || document.body.scrollTop;

        var time = setInterval(function() {
            a -= 50;
            document.documentElement.scrollTop = a;
            if (a <= 0) {
                clearInterval(time);
            }
        }, 20);
    };
}
//跳转注册页
let zhuce = document.getElementById("zhuce");
zhuce.onclick = function() {
    if (a) {
        location.href = 'login.html'
        localStorage.removeItem('token')
        localStorage.removeItem('username')
    } else {
        location.href = "register.html";
    }

};